package com.chainyoung.component_tmtrade.ui.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chainyoung.common.utils.ConvertUtils;
import com.chainyoung.common.utils.ScreenUtils;
import com.chainyoung.common.utils.StringUtil;
import com.chainyoung.component_tmtrade.R;
import com.chainyoung.component_tmtrade.R2;
import com.chainyoung.component_tmtrade.bean.TradeCurrentDetail;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.chainyoung.component_tmtrade.ui.fragment.fiatcurrency.TradeFiatCurrencyMainFragment.TYPE_BUY;
import static com.chainyoung.component_tmtrade.ui.fragment.fiatcurrency.TradeFiatCurrencyMainFragment.TYPE_SELL;


public class TradeCurrency2CurrencyAdapter extends BaseAdapter {


    private Context mContext;
    private List<TradeCurrentDetail> dataSet;
    private int type = TYPE_BUY;//
    private int width;


    public TradeCurrency2CurrencyAdapter(Context context, List<TradeCurrentDetail> datas) {
        this.mContext = context;
        this.dataSet = datas;
    }

    public TradeCurrency2CurrencyAdapter(Context context, List<TradeCurrentDetail> datas, int type) {
        this.mContext = context;
        this.dataSet = datas;
        this.type = type;
        width = (int) (ScreenUtils.getScreenWidth(mContext) * 3 / 7);
    }


    @Override
    public int getCount() {
        return dataSet.size();
    }

    @Override
    public Object getItem(int position) {
        return dataSet.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.tmtrade_list_trade_current_detail, null);
            convertView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ConvertUtils.dp2px(mContext, 30)));
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        TradeCurrentDetail item = type == TYPE_BUY ? dataSet.get(position) : dataSet.get(dataSet.size() - position - 1);
        viewHolder.tvNum.setTextColor(ContextCompat.getColor(mContext, R.color._7e8da6));
        String typeName = type == TYPE_BUY ? mContext.getString(R.string.tmtrade_buy_str) : mContext.getString(R.string.tmtrade_sell_str);
        if (type == TYPE_SELL) {
            viewHolder.tvType.setText(new StringBuilder().append(typeName).append(dataSet.size() - position).toString());
            //  viewHolder.tvType.setTextColor(ContextCompat.getColor(mContext, R.color._0AC073));
            viewHolder.tvPrice.setTextColor(ContextCompat.getColor(mContext, R.color._F95A54));
           // viewHolder.tvNum.setTextColor(ContextCompat.getColor(mContext, R.color._F95A54));
        } else {
            viewHolder.tvType.setText(new StringBuilder().append(typeName).append(position + 1).toString());
            // viewHolder.tvType.setTextColor(ContextCompat.getColor(mContext, R.color._F95A54));
            viewHolder.tvPrice.setTextColor(ContextCompat.getColor(mContext, R.color._0AC073));
            //viewHolder.tvNum.setTextColor(ContextCompat.getColor(mContext, R.color._0AC073));
        }
        BigDecimal bdPrice=new BigDecimal(item.getPrice());
        BigDecimal bdNum=new BigDecimal(item.getNums());
        viewHolder.tvPrice.setText(bdPrice.compareTo(BigDecimal.ZERO)<=0?"-":StringUtil.subZeroAndDot(item.getPrice().length()>8?item.getPrice().substring(0,8):item.getPrice()));
        viewHolder.tvNum.setText(bdNum.compareTo(BigDecimal.ZERO)<=0?"-":StringUtil.subZeroAndDot(item.getNums().length()>8?item.getNums().substring(0,8):item.getNums()));
      //  viewHolder.tvPrice.setText(StringUtil.subZeroAndDot(new BigDecimal(item.getPrice()).setScale(6, BigDecimal.ROUND_DOWN) + ""));
       // viewHolder.tvNum.setText(StringUtil.subZeroAndDot(new BigDecimal(item.getNums()).setScale(6, BigDecimal.ROUND_DOWN) + ""));
        double percent = (Double.valueOf(item.getNums()) / Double.valueOf(item.getOrigin_num()));
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) viewHolder.view.getLayoutParams();
        params.width = (int) (width * percent);
        viewHolder.view.setLayoutParams(params);
        viewHolder.view.setBackgroundColor(type == TYPE_BUY ? ContextCompat.getColor(mContext, R.color._ddf9ed) : ContextCompat.getColor(mContext, R.color._fdeded));
        return convertView;
    }


    static class ViewHolder {
        @BindView(R2.id.tv_type)
        TextView tvType;
        @BindView(R2.id.tv_price)
        TextView tvPrice;
        @BindView(R2.id.tv_num)
        TextView tvNum;
        @BindView(R2.id.view)
        View view;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
